package com.wc.算法提高课.D第四章_高级数据结构.平衡树.营业额统计;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/27 23:11
 * @description https://www.acwing.com/problem/content/description/267/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, INF = (int) 1e8;
    static Node[] tr = new Node[N];
    static int[] a = new int[N];
    static int n, root, idx = 0;

    public static void main(String[] args) {
        n = sc.nextInt();
        tr[0] = new Node();
        root = build();
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        root = insert(root, a[1]);
        long res = a[1];
        for (int i = 2; i <= n; i++) {
            res += Math.min(a[i] - getPrev(root, a[i]), getNext(root, a[i]) - a[i]);
            root = insert(root, a[i]);
        }
        out.println(res);
        out.flush();
    }

    static int getNext(int p, int x) {
        if (p == 0) return INF;
        if (tr[p].key < x) return getNext(tr[p].r, x);
        else return Math.min(tr[p].key, getNext(tr[p].l, x));
    }

    static int getPrev(int p, int x) {
        if (p == 0) return -INF;
        if (tr[p].key > x) return getPrev(tr[p].l, x);
        else return Math.max(tr[p].key, getPrev(tr[p].r, x));
    }

    static int getKeyByRank(int p, int x) {
        if (p == 0) return p;
        if (tr[tr[p].l].size >= x) return getKeyByRank(tr[p].l, x);
        if (tr[tr[p].l].size + tr[p].cnt >= x) return tr[p].key;
        return getKeyByRank(tr[p].r, x - tr[tr[p].l].size - tr[p].cnt);
    }

    static int getRankByKey(int p, int x) {
        if (p == 0) return p;
        if (tr[p].key == x) return tr[tr[p].l].size + 1;
        else if (tr[p].key > x) return getRankByKey(tr[p].l, x);
        else return tr[tr[p].l].size + tr[p].cnt + getRankByKey(tr[p].r, x);
    }

    static int remove(int p, int x) {
        if (p == 0) return 0;
        if (tr[p].key == x) {
            if (tr[p].cnt > 1) tr[p].cnt--;
            else if (tr[p].l != 0 || tr[p].r != 0) {
                if (tr[p].r == 0 || tr[tr[p].l].val > tr[tr[p].r].val) {
                    p = zig(p);
                    tr[p].r = remove(tr[p].r, x);
                } else {
                    p = zag(p);
                    tr[p].l = remove(tr[p].l, x);
                }
            } else return 0;
        } else if (tr[p].key > x) tr[p].l = remove(tr[p].l, x);
        else tr[p].r = remove(tr[p].r, x);
        pushUp(p);
        return p;
    }

    static int insert(int p, int x) {
        if (p == 0) return getNode(x);
        if (tr[p].key == x) tr[p].cnt++;
        else if (tr[p].key > x) {
            tr[p].l = insert(tr[p].l, x);
            if (tr[tr[p].l].val > tr[p].val) p = zig(p);
        } else {
            tr[p].r = insert(tr[p].r, x);
            if (tr[tr[p].r].val > tr[p].val) p = zag(p);
        }

        pushUp(p);
        return p;
    }

    static int build() {
        int root = getNode(-INF), right = getNode(INF);
        tr[root].r = right;
        if (tr[root].val < tr[right].val) root = zag(root);
        pushUp(root);
        return root;
    }

    static int zig(int p) {
        int q = tr[p].l;
        tr[p].l = tr[q].r;
        tr[q].r = p;
        p = q;
        pushUp(tr[p].r);
        pushUp(p);
        return p;
    }

    static int zag(int p) {
        int q = tr[p].r;
        tr[p].r = tr[q].l;
        tr[q].l = p;
        p = q;
        pushUp(tr[p].l);
        pushUp(p);
        return p;
    }

    static void pushUp(int p) {
        tr[p].size = tr[p].cnt + tr[tr[p].l].size + tr[tr[p].r].size;
    }

    static int getNode(int x) {
        tr[++idx] = new Node();
        tr[idx].cnt = tr[idx].size = 1;
        tr[idx].key = x;
        tr[idx].val = (int) (Math.random() * INF) + 1;
        return idx;
    }


    static class Node {
        int l, r;
        int key, val;
        int cnt, size;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
